package com.kurumi.medical.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;




public class FileUtil {

	public static boolean createOrEditFile(String data,String fileDir,String fileName) throws IOException{
		File destFile = new File(fileDir,fileName);
		if(!destFile.exists()){
			File destDir = new File(fileDir);
			if(!destDir.exists()){
				destDir.mkdirs();
			}
			destFile.createNewFile();
		}
		
		FileOutputStream fileOutputStream = null;
		OutputStreamWriter outputWriter = null;
		
		try {
			fileOutputStream = new FileOutputStream(destFile);  
	        outputWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");  
	        outputWriter.write(data);
		} finally {
			IOUtils.closeQuietly(outputWriter);
			IOUtils.closeQuietly(fileOutputStream);
		}
      
		return true;
	}
	
	
	public static String readLineFile(String filePath, String fileName) {
		String jsonStr = null;
		BufferedReader br = null;
		try {
			File destFile = new File(filePath, fileName);
			if (destFile.exists()) {
				// FileReader fr = new FileReader(destFile);
				br = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath + fileName), "UTF-8"));
				jsonStr = br.readLine();
				
				

			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return jsonStr;
	}
	
	public static Map<String, Object> readLineFile1(String filePath, String fileName) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		try {
			File destFile = new File(filePath, fileName);
			String jsonStr = null;
			if (destFile.exists()) {
				// FileReader fr = new FileReader(destFile);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath + fileName), "UTF-8"));
				jsonStr = br.readLine();
				jsonMap = JsonUtil.jsonToPojo(jsonStr, Map.class);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonMap;
	}
	
	public static void deleleDirAllFile(String dirName) {
		try {
			File dirFile = new File(dirName);
			
			if (dirFile.isDirectory()) {
				File[] files = dirFile.listFiles();
				
				if (null != files && files.length > 0) {
					for (File file : files) {
						try {
							file.delete();
						} catch (Exception e) {
						}
					}
				}
				
				dirFile.delete();
			}
		} catch (Exception e) {
		}
		
	}
	
   public static Result copyFiles(List<String> fileNames, String destPath, String localTempProgressNoteFolder, String destDir) {
	   List<String> destFileNames = new ArrayList<String>();
	   boolean flag = true;
	   String msg = "";
	   
		   if (null != fileNames && fileNames.size() > 0) {
			   for (String filePath : fileNames) {
				   File sourceFile = new File(localTempProgressNoteFolder + File.separator + filePath);
				   File destFile = new File(destPath + File.separator + sourceFile.getName());
				   FileInputStream inputStream = null;
				   FileOutputStream outputStream = null;
				   
				try {
					inputStream = new FileInputStream(sourceFile);
					outputStream = new FileOutputStream(destFile);
					IOUtils.copy(inputStream, outputStream);
					destFileNames.add(destDir + File.separator + sourceFile.getName());
				} catch (Exception e) {
					e.printStackTrace();
					msg = ExceptionUtil.getStackTrace(e);
					flag = false;
				} finally {
					IOUtils.closeQuietly(inputStream);
					IOUtils.closeQuietly(outputStream);
				}
			   }
		   }
	   return new Result(flag, msg, destFileNames);
   }
	
  
	
	
}











